package com.duqc.web.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
 * @ClassName AsyncTasksServiceImpl
 * @Description AsyncTasksServiceImpl
 * @Author duqc
 * @Date 2021/9/14
 **/
@Slf4j
@Component
public class AsyncTasksServiceImpl {

    public static Random random = new Random();

    @Async
    public CompletableFuture<String> doTaskOne() throws InterruptedException {
        log.info("开始做任务1");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTime = System.currentTimeMillis();
        log.info("完成任务1，耗时：" + (endTime - startTime) + "毫秒");
        return CompletableFuture.completedFuture("任务1完成");
    }

    @Async
    public CompletableFuture<String> doTaskTwo() throws InterruptedException {
        log.info("开始做任务2");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTime = System.currentTimeMillis();
        log.info("完成任务2，耗时：" + (endTime - startTime) + "毫秒");
        return CompletableFuture.completedFuture("任务2完成");
    }

    @Async
    public CompletableFuture<String> doTaskThree() throws InterruptedException {
        log.info("开始做任务3");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTime = System.currentTimeMillis();
        log.info("完成任务3，耗时：" + (endTime - startTime) + "毫秒");
        return CompletableFuture.completedFuture("任务3完成");
    }

}
